def writeStatusMessage(msg):
    print '<div class="message">', msg, '</div>';

def writeCode(code):
    print '<pre>', code, '</pre>';


from logger import *
def writeLog(logger):
    print '<table border=1 class="log" width=800>';

    print '<tr>';
    print '<th>Step</th>';
    print '<th>Description</th>';
    print '<th>SQL</th>';
    print '<th>Result</th>';
    print '</tr>';

    for i in range(logger.getNumMessages()):
        msg = logger.getMessage(i);

        print '<tr>';

        print '<td><center>', i+1, '</center></td>';
        print '<td>', msg.description, '</td>';
        print '<td>', msg.sql, '</td>';
        print '<td>', msg.results, '</td>';
        print '</tr>';

    print '</table>';
    print '<p>';
    print 'Final Answer: ', logger.getFinalAnswer();


def writeObjects(objs):
    print '<table border=1>';

    print '<tr><th>Object ID</th><th>English Translation</th></tr>';

    for obj in objs:
        print '<tr>';
        print '<td>', obj[0], '</td>';
        print '<td>', obj[1], '</td>';        
        print '</tr>';
    print '</table>';

    




def writeTable(db, tableName):
    
    sql = "SELECT * FROM `" + tableName +"`";
    cursor = db.cursor();

    print sql;
    cursor.execute(sql);
    result = cursor.fetchall();
    fields = cursor.description;

    print '<table border=1>';
    print '<tr>';
    for field in fields:
        print '  <th>' + field[0] + '</th>';

    print '</tr>';
    
    for record in result:
        print '<tr>';
        for i in range(len(record)):
            val = record[i];
            if (val == None):
                str = "&nbsp;";
            else:
                str = record[i];
                
            print '  <td>', str, '</td>';

        print '</tr>';

    print '</table>';
